//
//  HHBaseTabController.m
//  Flower
//
//  Created by 花花 on 2016/12/2.
//  Copyright © 2016年 花花. All rights reserved.
//

#import "HHBaseTabController.h"

@interface HHBaseTabController ()

@end

@implementation HHBaseTabController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];
    self.tableView.rowHeight = 55;
    
}


#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.titles.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *cellid = @"cellid";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellid];
        
    }
    cell.textLabel.text =[NSString stringWithFormat:@"%zd - %@",indexPath.row+1,self.titles[indexPath.row]];
    cell.detailTextLabel.text = self.classNames[indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSString *className = self.classNames[indexPath.row];
    
    //
    NSUInteger classNameLength = className.length;
    NSUInteger storyboardlength = @"_UIStoryboard".length;
    NSUInteger xiblength = @"_xib".length;
    
    NSString *suffixClassName;
    
    if (classNameLength >storyboardlength) {
        suffixClassName = [className substringFromIndex:classNameLength - storyboardlength];
    }
    
    
    if ([suffixClassName isEqualToString:@"_UIStoryboard"]) {
        className = [className substringToIndex:classNameLength - storyboardlength];
        
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:className bundle:[NSBundle mainBundle]];
        [self.navigationController pushViewController:[storyboard  instantiateInitialViewController] animated:YES];
        
    }else if ([[className substringFromIndex:classNameLength - xiblength] isEqualToString:@"_xib"]) {
        
        className = [className substringToIndex:classNameLength - xiblength];
        
        UIViewController *vc = [[NSClassFromString(className) alloc]initWithNibName:className bundle:nil];
        vc.title = self.titles[indexPath.row];
        [self.navigationController pushViewController:vc animated:YES];
        
    }else {
        
        UIViewController *vc = [[NSClassFromString(className) alloc] init];

        vc.title = self.titles[indexPath.row];
        [self.navigationController pushViewController:vc animated:YES];
    }
    
}

@end
